<template>
  <div
    ref="rightPanel"
    :class="{ show: show }"
    class="rightPanel-container"
    :style="{ 'max-width': max_width }"
  >
    <div
      :class="useModal ? 'rightPanel-background' : ''"
      :style="{ 'z-index': zIndex }"
    />
    <div class="rightPanel" :style="{ 'z-index': zIndex }">
      <div
        :class="show ? 'handle-button' : 'handle-button-display'"
        :style="{ top: buttonTop + 'px', 'background-color': theme }"
        @click="close"
      >
        <i class="el-icon-close" />
      </div>
      <div class="rightPanel-items" v-if="loadItem">
        <slot v-if="show"></slot>
      </div>
    </div>
  </div>
</template>

<script>
import { addClass, removeClass, getMaxIndex } from '@/utils'

export default {
  name: 'RightPanel',
  props: {
    clickNotClose: {
      default: false,
      type: Boolean
    },
    buttonTop: {
      default: 250,
      type: Number
    },
    modal: {
      default: true,
      type: Boolean
    },
    maxWidth: {
      default: 300,
      type: Number
    }
  },
  data () {
    return {
      zIndex: getMaxIndex(),
      show: false,
      loadItem: false,
      useModal: this.modal,
      max_width: this.maxWidth
    }
  },
  computed: {
    theme () {
      return this.$store.state.settings.theme
    }
  },
  created () {
    this.$bus.on('open-rightPanel', this.showPanel)
  },
  watch: {
    show (nVal) {
      this.loadItem = nVal
    }
  },
  mounted () {
    this.insertToBody()
  },
  beforeDestroy () {
    this.$bus.off('open-rightPanel')
    const elx = this.$refs.rightPanel
    elx.remove()
  },
  methods: {
    showPanel (show) {
      this.show = show
      if (this.show) {
        const useModel = document.querySelector('.rightPanel-background')
        if (useModel) {
          if (!this.clickNotClose) {
            this.addEventClick(useModel)
          } else {
            this.removeEventClick(useModel)
          }
        }
      }
      if (this.show) {
        addClass(document.body, 'showRightPanel')
      } else {
        removeClass(document.body, 'showRightPanel')
      }
    },
    addEventClick (element) {
      element.addEventListener('click', this.close)
    },
    close () {
      this.show = false
    },
    removeEventClick (element) {
      element.removeEventListener('click', this.close)
    },
    insertToBody () {
      const elx = this.$refs.rightPanel
      const body = document.querySelector('body')
      body.insertBefore(elx, body.firstChild)
    }
  }
}
</script>

<style>
.showRightPanel {
  overflow: hidden;
  position: relative;
  width: calc(100% - 15px);
}
</style>

<style lang="scss" scoped>
.handle-button-display {
  display: none;
}
.rightPanel-background {
  position: fixed;
  top: 0;
  left: 0;
  opacity: 0;
  transition: opacity 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);
  background: rgba(0, 0, 0, 0.2);
  z-index: -1;
}

.rightPanel {
  // width: 100%;
  max-width: 700px;
  height: 100vh;
  position: fixed;
  top: 0;
  right: 0;
  box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.05);
  transition: all 0.25s cubic-bezier(0.7, 0.3, 0.1, 1);
  transform: translate(100%);
  background: #fff;
}

.show {
  transition: all 0.3s cubic-bezier(0.7, 0.3, 0.1, 1);

  .rightPanel-background {
    z-index: 2029;
    opacity: 1;
    width: 100%;
    height: 100%;
  }

  .rightPanel {
    transform: translate(0);
  }
}

.handle-button {
  width: 48px;
  height: 48px;
  position: absolute;
  left: -48px;
  text-align: center;
  font-size: 24px;
  border-radius: 6px 0 0 6px !important;
  z-index: 0;
  pointer-events: auto;
  cursor: pointer;
  color: #fff;
  line-height: 48px;
  i {
    font-size: 24px;
    line-height: 48px;
  }
}
</style>
